package com.dimeng;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.dimeng.utils.CommonUtil;
import com.dimeng.utils.Constant;
import com.dimeng.utils.LoadDataThread;

/**
 * @author write by ccn<br>
 * <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-7-11 上午11:19:56<br>
 * descrition:反馈建议视图
 */
public class SuggestActivity extends SuperActivity {
	private static final long serialVersionUID = -2152168480714229569L;
	private ImageButton submitButton;
	private EditText suggestContentEditText;
	private EditText emailEditText;
	private Thread submitThread;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(SuggestActivity.this, "提交成功,非常感谢您提供的宝贵意见", Toast.LENGTH_LONG).show();
				SuggestActivity.this.finish();
				break;
			case 2:
				Toast.makeText(SuggestActivity.this, Constant.NET_ERROR, Toast.LENGTH_LONG).show();
			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.suggest);
		submitButton = (ImageButton) findViewById(R.id.submitButton);
		CommonUtil.setButtonFocusChanged(submitButton);
		suggestContentEditText = (EditText) findViewById(R.id.suggestContentId);
		emailEditText = (EditText) findViewById(R.id.emailId);
		
		submitButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String suggestContent = suggestContentEditText.getText().toString().trim();
				if("".equals(suggestContent)){
					Toast.makeText(SuggestActivity.this, "内容不能为空,请填写您的意见", Toast.LENGTH_SHORT).show();
					suggestContentEditText.setFocusable(true);
					return;
				}
				String emailAddr = emailEditText.getText().toString().trim();
				try {
					suggestContent = "来自Android手机客户端:" + suggestContent;
					suggestContent = URLEncoder.encode(suggestContent,Constant.CHARSET_GBK);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				String url = Constant.HOST_URL+"suggest.do?suggestContent="+suggestContent+"&email="+emailAddr;
				startThread(url);
			}
		});
	}
	private void startThread(String url){
		if(null == submitThread || !submitThread.isAlive()){
			submitThread = new LoadDataThread(url,Constant.CHARSET_GBK,handler,false);
			submitThread.start();
		}
	}
}
